package se.hiq.wineo.client.widgets;

import se.hiq.wineo.client.widgets.common.EnumFieldWidget;
import se.hiq.wineo.shared.data.WineColor;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;

public class WineFilterWidget extends Composite implements WineFilter.View {

	// Widgets in this Composite
	private Grid mainPanel;
	private Label searchLabel;
	private Label nameLabel;
	private Label yearLabel;
	private Label colorLabel;
	private TextBox nameBox;
	private TextBox yearBox;
	private EnumFieldWidget<WineColor> colorBox;
	private Button searchButton;
	private Grid resultPanel; 
	private Label numResultsLabel;
	private Label numResultsValue;
	
	public WineFilterWidget() {
		mainPanel = new Grid(4, 4);
		mainPanel.setStyleName("wineFilterWidget");
		searchLabel = new Label("Filtrering");
		searchLabel.setStyleName("wineHeading");
		nameLabel = new Label("Namn");
		yearLabel = new Label("År");
		colorLabel = new Label("Färg");
		nameBox = new TextBox();
		yearBox = new TextBox();
		colorBox = new EnumFieldWidget<WineColor>(null, WineColor.values());
		searchButton = new Button("Hitta viner");
		resultPanel = new Grid(1, 2);
		numResultsLabel = new Label("Antal träffar:");
		numResultsValue = new Label("0");
		resultPanel.setWidget(0, 0, numResultsLabel);
		resultPanel.setWidget(0, 1, numResultsValue);
		
		mainPanel.setWidget(0, 0, searchLabel);
		mainPanel.setWidget(1, 0, nameLabel);
		mainPanel.setWidget(2, 0, nameBox);
		mainPanel.setWidget(1, 1, yearLabel);
		mainPanel.setWidget(2, 1, yearBox);
		mainPanel.setWidget(1, 2, colorLabel);
		mainPanel.setWidget(2, 2, colorBox);
		mainPanel.setWidget(2, 3, searchButton);
		mainPanel.setWidget(3, 0, resultPanel);
		
		initWidget(mainPanel);
	}

	@Override
	public void attach(Panel panel) {
		panel.add(this);
	}

	@Override
	public HasClickHandlers getFilterButton() {
		return searchButton;
	}

	@Override
	public HasValue<String> getName() {
		return nameBox;
	}

	@Override
	public HasText getNumResults() {
		return numResultsValue;
	}

	@Override
	public HasValue<String> getYear() {
		return yearBox;
	}
	
	@Override
	public HasValue<WineColor> getColor() {
		return colorBox;
	}
	
	@Override
	public void isFiltering(boolean filtering) {
		searchButton.setEnabled(!filtering);
	}
	
	@Override
	public void displayError(String errorMessage) {
		Window.alert(errorMessage);
	}
}
